const http = require("http");
const url = require("url");
const querystring = require('querystring') 
const fs = require("fs");
const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');


var transport = nodemailer.createTransport(smtpTransport({
  host: "10.141.72.4", // 主机
  secure: false, // 使用 SSL有问题
  port: 25, // SMTP 端口
  auth: {
    user: "zhangbin@chinatelecom.cn", // 账号
    pass: "1728essl" // 密码
  }
}));

var mail = {
  from: "zhangbin@chinatelecom.cn", // 发件地址
  to: "bb_boy@163.com", // 收件列表
  subject: "Hello world", // 标题
  text:"hello",
  html: "<b>世界，你好！</b> " // html 内容
}


http.createServer(function(request,response){
	response.writeHead(200,{"Content-type": "text/plain;charset=utf-8"});
	
	var result="server ok!";	
	var qs=querystring.parse(url.parse(request.url).query);
	if(qs!=null )
	{
		if(qs.name!=null)
		{
            fs.stat(qs.name,function(error, stats){
				if (error) 
				{
					result="文件 '"+qs.name+"' 信息检索失败:"+error;
					
				} else 
				{
					result="文件 '"+qs.name+"' 大小:"+stats.size+ " bytes";
				}
				response.end(result);
			});
		}
		else if(qs.mail!=null)
		{
		    mail.subject=qs.mail;
            transport.sendMail(mail,function(error,info)
			{
				if (error) 
				{
					result="邮件发送失败:"+error;
					
				} else 
				{
					result="邮件发送成功！"+info.messageId;
				}
				response.end(result);
			});
		}
		else
		  response.end(result);
   } 
   else
	 response.end(result);
	
}).listen(8888);


console.log("server running at http://127.0.0.1:8888/");
